package com.agile.leetcode.PriorityQueue;

import java.util.PriorityQueue;

/**
 * 使用优先队列来找出数组中倒数第K大的元素
 *
 * @Author:ChenZhangKun
 * @Date: 2021/5/26 22:29
 */
public class PriorityQueueTest5 {
    public static void main(String[] args) {
        int[] nums = {
                1, 2, 3, 4545, 4, 8, 2, 9, 4, 24, 24, 8, 2, 81, 5, 8, 5, 1, 0, 235, 1, 45, 0, 3, 45, 7, 0, 69, 0, 55, 23, 4, 5, 0
        };
        PriorityQueue priorityQueue = new PriorityQueue();
        // 遍历数组
        for (int num : nums) {
            priorityQueue.add(num);
            if (priorityQueue.size() > 5) {
                // 弹出栈顶
                System.out.println(priorityQueue.poll());
            }
        }
        System.out.println(priorityQueue.peek());
    }
}
